package memory

import (
	"context"
	"sync"
	"time"

	"github.com/go-redis/redis/v8"
)

type MemWrapper struct {
	DbInstance *redis.Client
	ctx        context.Context
}

func NewMem() *MemWrapper {
	obj := new(MemWrapper)

	return obj
}

var instance *MemWrapper
var once sync.Once

func GetInstance() *MemWrapper {
	once.Do(func() {
		instance = NewMem()
	})
	return instance
}

func init() {

}

func (dbw *MemWrapper) Connect(dsn string, defaultdb int) {
	dbw.DbInstance = redis.NewClient(&redis.Options{
		Addr:     dsn,
		Password: "",        // no password set
		DB:       defaultdb, // use default DB
	})
	dbw.ctx = context.Background()
}

func (dbw *MemWrapper) Set(key string, val interface{}, to time.Duration) {
	dbw.DbInstance.Set(dbw.ctx, key, val, to)
}

func (dbw *MemWrapper) Get(key string) *redis.StringCmd {
	return dbw.DbInstance.Get(dbw.ctx, key)
}

func (dbw *MemWrapper) Del(key string) {
	dbw.DbInstance.Del(dbw.ctx, key)
}
